package cn.dnf.order.service.impl;

import cn.dnf.basic.service.impl.BaseServiceImpl;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.PageList;
import cn.dnf.order.domain.OrderPetAcquisition;
import cn.dnf.order.dto.OrderDto;
import cn.dnf.order.mapper.OrderPetAcquisitionMapper;
import cn.dnf.order.query.OrderPetAcquisitionQuery;
import cn.dnf.order.service.IOrderPetAcquisitionService;
import cn.dnf.org.domain.Employee;
import cn.dnf.org.domain.Shop;
import cn.dnf.org.mapper.EmployeeMapper;
import cn.dnf.org.mapper.ShopMapper;
import cn.dnf.pet.domain.Pet;
import cn.dnf.pet.mapper.PetMapper;
import cn.dnf.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(@RequestBody OrderPetAcquisitionQuery query, Long id) {
        Employee employee = employeeMapper.loadByLogininfoId(id);
        if(employee.getShop_id()!=null){
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdminId()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
            //店铺人员
            query.setShopId(employee.getShop_id());

        }

        return super.pageQuery(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        /**
         * 1.修改订单状态,金额
         * 2.改寻主消息状态
         * 3.改pet金额
         */
        //1.
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(orderId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //2.
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearch_master_msg_id());
        //3.
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    @Override
    public void cancel(Long id) {
        /**
         * 1.修改订单状态
         * 2.寻主消息的shopid=null
         * 3.pet删除
         */
        //1.
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);//取消
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //2
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearch_master_msg_id());
        //3.
        petMapper.remove(orderPetAcquisition.getPet_id());
    }
}
